package com.shinnawy.ispeaka;

import android.app.Notification;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.app.Service;
import android.appwidget.AppWidgetManager;
import android.content.ComponentName;
import android.content.Intent;
import android.os.IBinder;
import android.view.Gravity;
import android.widget.RemoteViews;
import android.widget.Toast;

public class iSpeakaService extends Service {

	@Override
	public void onCreate() {
		Logger.Log(this, "onCreate");
		mNM = (NotificationManager)getSystemService(NOTIFICATION_SERVICE);
		Notification notification = new Notification(R.drawable.icon, "iSpeaka is running!", System.currentTimeMillis());
		notification.defaults |= Notification.DEFAULT_VIBRATE;
		long[] vibrate = {0 ,100, 200, 300};
		notification.vibrate = vibrate;
		notification.flags = Notification.FLAG_NO_CLEAR | Notification.FLAG_ONGOING_EVENT;
		// Create an Intent to launch toggle state
		Intent toggleIntent = new Intent(this, iSpeakaService.class);
		toggleIntent.setAction(iSpeakaService.INTENT_UPDATE_STATE);
		PendingIntent pendingIntent = PendingIntent.getService(this, 0, toggleIntent, 0);
		CharSequence title = "iSpeaka";
		CharSequence desc = "iSpeaka is running, click to disable!";
		notification.setLatestEventInfo(this, title, desc, pendingIntent);
		this.startForeground(666, notification);
		mActive = false;
		drawState();
	}
	
	@Override
	public int onStartCommand(Intent intent, int flags, int startId) {
		Logger.Log(this, "onStart");

		Logger.Log(this, "onStart received intent (" + intent.getAction() + ") startId (" + startId + ")");
		if(intent.getAction() != null && intent.getAction().equals(iSpeakaService.INTENT_UPDATE_STATE))
			toggleActive();
		else
			drawState();

		RemoteViews view = new RemoteViews(this.getPackageName(), R.layout.main);

		// Create an Intent to launch toggle state
		Intent toggleIntent = new Intent(this, iSpeakaService.class);
		toggleIntent.setAction(iSpeakaService.INTENT_UPDATE_STATE);
		PendingIntent pendingIntent = PendingIntent.getService(this, 0, toggleIntent, 0);
		// Get the layout for the App Widget and attach an on-click listener to the icon
		view.setOnClickPendingIntent(R.id.icon, pendingIntent);

		// Push update for this widget to the home screen
		ComponentName thisWidget = new ComponentName(this, iSpeakaWidget.class);
		AppWidgetManager manager = AppWidgetManager.getInstance(this);
		manager.updateAppWidget(thisWidget, view);
		
		return START_STICKY;
	}

	@Override
	public void onDestroy() {
		Logger.Log(this, "onDestroy");
		iSpeakaHardwareManager.getInstance().stop();
		mActive = false;
		drawState();
		stopForeground(true);
	}

	@Override
	public IBinder onBind(Intent intent) {
		return null;
	}
	
	@Override
	public void onLowMemory() {
		Logger.Log(this, "onLowMemory");
		iSpeakaHardwareManager.getInstance().stop();
		mActive = false;
		drawState();
	}

	private void toggleActive() {
		Logger.Log(this, "toggleActive");
		if(iSpeakaWidget.WIDGET_CONTEXT == null)
		{
			Logger.Log(this, "WIDGET_CONTEXT = null, return");
			return;
		}
		RemoteViews view = new RemoteViews(this.getPackageName(), R.layout.main);
		if(view == null)
		{
			Logger.Log(this, "view = null, return");
			return;
		}
		mActive = !mActive;
		if(mActive)
		{
			iSpeakaHardwareManager.getInstance().start();
			view.setImageViewResource(R.id.icon, R.drawable.speakeron);
			Toast msg = Toast.makeText(iSpeakaWidget.WIDGET_CONTEXT, R.string.service_enabled, 3000);
			msg.setGravity(Gravity.CENTER, msg.getXOffset() / 2, msg.getYOffset() / 2);
			msg.show();
		}
		else
		{
			iSpeakaHardwareManager.getInstance().stop();
			view.setImageViewResource(R.id.icon, R.drawable.speakeroff);
			Toast msg = Toast.makeText(iSpeakaWidget.WIDGET_CONTEXT, R.string.service_disabled, 3000);
			msg.setGravity(Gravity.CENTER, msg.getXOffset() / 2, msg.getYOffset() / 2);
			msg.show();
			stopSelf();
			mNM.cancel(0);
		}

		// Push update for this widget to the home screen
		ComponentName thisWidget = new ComponentName(this, iSpeakaWidget.class);
		AppWidgetManager manager = AppWidgetManager.getInstance(this);
		manager.updateAppWidget(thisWidget, view);
	}

	private void drawState() {
		Logger.Log(this, "drawState");
		RemoteViews view = new RemoteViews(this.getPackageName(), R.layout.main);
		if(view == null)
		{
			Logger.Log(this, "view = null, return");
			return;
		}
		if(mActive)
			view.setImageViewResource(R.id.icon, R.drawable.speakeron);
		else
			view.setImageViewResource(R.id.icon, R.drawable.speakeroff);

		// Push update for this widget to the home screen
		ComponentName thisWidget = new ComponentName(this, iSpeakaWidget.class);
		AppWidgetManager manager = AppWidgetManager.getInstance(this);
		manager.updateAppWidget(thisWidget, view);
	}

	private static boolean mActive;
	private NotificationManager mNM;

	public static final String INTENT_UPDATE_STATE = "com.shinnawy.ispeaka.INTENT_UPDATE_STATE";
}
